package com.example.blades.myalarmservice;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import java.util.Calendar;

public class AlarmServiceActivity extends AppCompatActivity implements View.OnClickListener {

    private static AlarmServiceActivity appRef = null;
    private Button btn_call, btn_exit;
    boolean k = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appRef = this;
        setContentView(R.layout.activity_alarm_service);

        btn_call = (Button) findViewById(R.id.btn_call);
        btn_exit = (Button) findViewById(R.id.btn_exit);
        btn_call.setOnClickListener(this);
        btn_exit.setOnClickListener(this);
    }

    public static AlarmServiceActivity getApp()
    {
        return appRef;
    }

    public void btnEvent(String data)
    {
        setTitle(data);
    }

    public void onClick(View arg0)
    {
        if (arg0==btn_call)
        {
            setTitle("Waiting... Alarm=5");
            Intent intent=new Intent(AlarmServiceActivity.this,AlarmReceiver.class);

            PendingIntent p_intent=PendingIntent.getBroadcast(AlarmServiceActivity.this,0,intent,0);
            Calendar calendar=Calendar.getInstance();//get a Calendar Object
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND,3);

            //Schedule the alarm
            AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),p_intent);
        }

        if(arg0==btn_exit)
        {
            Intent intent=new Intent(AlarmServiceActivity.this,AlarmReceiver.class);
            PendingIntent p_intent=PendingIntent.getBroadcast(AlarmServiceActivity.this,0,intent,0);
            AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(p_intent);
            finish();
        }


    }

}
